package com.claude.mainactivity;

import org.json.JSONObject;

import com.android.volley.VolleyError;
import com.claude.bean.Version;
import com.claude.communication.RequestManager;
import com.claude.communication.RequestNode;
import com.claude.communication.RequestNode.RequestListener;
import com.claude.update.UpdateApp;
import com.claude.util.Check;
import com.claude.util.Configure;
import com.claude.util.Parser;
import com.special.ResideMenuDemo.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.Toast;

public class LoadingCheckActivity extends Activity {

	private final int UPDATE_REQUEST = 0;
	private MyApplication mMyApplication = null;
	private RequestManager mRequestManager = null;
	private RequestListener mRequestListener = null;
	private Configure configure = null;
	private boolean mState = false;
	private UpdateApp mUpdateApp = null;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_loadingcheck);

		mMyApplication = (MyApplication) getApplication();
		configure = Configure.getInstance();
		
		checkState();
		
		if (mMyApplication.getOfflineState()) {
			checkVersion();
		}else{
			gotoLogin();
		}
	}
	
	public void onStop(){
		if(mRequestManager != null){
			mRequestManager.stopRequest();
		}
		super.onStop();
		this.finish();
	}

	private void checkVersion() {
		mRequestListener = new MyRequestListener();
		mRequestManager = RequestManager.newInstance();
		RequestNode requestNode = new RequestNode(configure.getUpdateUrl(),
				mRequestListener, null, UPDATE_REQUEST);
		mRequestManager.addRequest(requestNode);
		mRequestManager.startRequest();
	}

	private void checkState() {
		mState = Check.isConnected(LoadingCheckActivity.this);
		if (mState == false) {
			Toast.makeText(LoadingCheckActivity.this, "没有网络", 4000).show();
		}
		mMyApplication.setOfflineState(mState);
	}
	
	private void updateNow(){
		new AlertDialog.Builder(LoadingCheckActivity.this).setMessage("有新版需要更新")
		.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = new Intent();
				intent.setClass(LoadingCheckActivity.this, UpdateActivity.class);
				LoadingCheckActivity.this.startActivity(intent);
			}
		})
		.create().show();
		
	}
	
	private void selectUpdate(){
		new AlertDialog.Builder(LoadingCheckActivity.this).setMessage("该版本为测试版本，是否需要更新？")
		.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = new Intent();
				intent.setClass(LoadingCheckActivity.this, UpdateActivity.class);
				LoadingCheckActivity.this.startActivity(intent);				
			}
		})
		.setNegativeButton("取消", null)
		.create().show();
	}
	
	private void gotoLogin(){
		Intent intent = new Intent();
		intent.setClass(LoadingCheckActivity.this, LoginActivity.class);
		LoadingCheckActivity.this.startActivity(intent);
	}

	class MyRequestListener implements RequestListener {
		public void onSuccess(JSONObject jsonObject, int tag) {
			Version version = Parser.getVersion(jsonObject);
			// version is null
			mMyApplication.setVersion(version);
			System.out.println(version.getVersion());
			if (version != null) {
				mUpdateApp = new UpdateApp(mMyApplication,
						Double.parseDouble(version.getVersion()));
				switch (mUpdateApp.neadUpdate()) {
				case UpdateApp.UPDATE:
					System.out.println("s");
					updateNow();
					break;
				case UpdateApp.NO_UPDATE:
					System.out.println("ss");
					gotoLogin();
					break;
				case UpdateApp.CHOSE_UPDATE:
					System.out.println("sss");
					selectUpdate();
					break;
				}
			}
		}
		public void onError(VolleyError volleyError, int tag) {
			Toast.makeText(LoadingCheckActivity.this, "启动更新失败", 4000).show();
		}
	}
}
